<?php
	$validation_rules = array(
		"path" => array(
			"required" => true,
            #'path' => true,
            'maxlength' => 255
		),
		"title_en" => array(
            'maxlength' => 255
		),
		"title" => array(
            'maxlength' => 255
		),
		"other_language_episodes" => array(
			"number" => true,
            'maxlength' => 9
        ),

// bug 3979        
//		"average_rating" => array(
//            'number' => true,
//            'maxlength' => 255
//		),
        
		"first_time_onair[]" => array(
		),
		"publish_datetime[]" => array(
			"required" => true
		),
		"expire_datetime[]" => array(
			'required' => true
		)
	);
?>
<?php

    if ($act==='view') {
        FormHelper::setReadonly(true);
        $validation_rules = array();
    }
$from    = isset($data['from']) ? $data['from'] : '';
$episode = isset($data['episode']) ? $data['episode'] : array();
$episode_diff = isset($data['episode_diff']) ? $data['episode_diff'] : array();
$programme = isset($data['programme']) ? $data['programme'] : array();
$video_folder_list = isset($data['video_folder_list']) ? $data['video_folder_list'] : array();

$image_folder_list = isset($data['image_folder_list']) ? $data['image_folder_list'] : array();

//$tag_list = isset($data['tag_list']) ? $data['tag_list'] : array();
$arr_cast  = isset($data['arr_cast']) ? $data['arr_cast'] : array();
$arr_crew  = isset($data['arr_crew']) ? $data['arr_crew'] : array();

$cast_list = isset($data['cast_list']) ? $data['cast_list'] : array();
foreach ($cast_list as $key1=>$value1) {
	foreach ($value1 as $key2=>$value2) {
		$cast_list[$key1][$key2]['last_name']  = $value2['last_name'];
		$cast_list[$key1][$key2]['first_name'] = $value2['first_name'];
	}
}

//$remote_cast_list = isset($data['remotecast_list']) ? $data['remotecast_list'] : array();
$crew_list = isset($data['crew_list']) ? $data['crew_list'] : array();
foreach ($crew_list as $key1=>$value1) {
	foreach ($value1 as $key2=>$value2) {
		$crew_list[$key1][$key2]['last_name']  = $value2['last_name'];
		$crew_list[$key1][$key2]['first_name'] = $value2['first_name'];
	}
}

//$remote_crew_list = isset($data['remotecrew_list']) ? $data['remotecrew_list'] : array();
$cast_highlight = isset($data['highlight_cast']) ? $data['highlight_cast'] : array();
$crew_highlight = isset($data['highlight_crew']) ? $data['highlight_crew'] : array();
$form_action = ($act === 'view') ? "episode_switch.html" : "episode_step2.html?act=".$act;
$publish = isset($data['unpublish']) ? $data['unpublish'] : 0;

$en_language_id = language::getLanguageID(AC::LANGUAGE_EN);
?>

<div id="episode-step2" class="mod ui-widget-content ui-helper-clearfix ui-corner-all <?php echo $act; ?>">

    <div class="hd ui-widget-header ui-helper-clearfix ui-corner-all"><h2><?php echo $act_text; ?> Episode</h2></div>
	<div class="bd">
	<?php FormHelper::StartForm('ane', AC::PDES_BASE_URL.$form_action) ?>
	    <?php FormHelper::HiddenField('is_folder_update', '0') ?>
        <?php FormHelper::ErrorMessages($errors) ?>
        <fieldset class="ui-widget-content">
            <legend class="ui-widget-header">Find Programme Record</legend>
            <?php FormHelper::ProtectedField($programme->id, 'Programme ID');?>
            <?php FormHelper::ProtectedField($episode->id, 'Episode ID');?>
			<?php 
				FormHelper::ProtectedField($programme->zh_programme_name, 
										   'Programme Title (Chinese)');
				FormHelper::ProtectedField($programme->en_programme_name, 
										   'Programme Title (English)');
				FormHelper::ProtectedField($programme->path, 'Programme Path');
			?>
            <?php 
                if (isset($data['publishLog']->status)) {
                    FormHelper::HiddenField('onair_episode_no', $episode->onair_episode_no);
                    FormHelper::ProtectedField($episode->onair_episode_no, "On Air Episode No.", false);
                } else {
                    FormHelper::TextField('onair_episode_no', $episode->onair_episode_no, "On Air Episode No.", '', 
                                          'number', 1);
                }
            ?>
			<?php FormHelper::HiddenField('programme_id', $episode->programme_id) ?>
	    </fieldset>
    	<fieldset class="ui-widget-content">
    		<legend class="ui-widget-header">Episode Basic Information</legend>
			<?php FormHelper::HiddenField('id', $episode->id) ?>
			<?php FormHelper::HiddenField('language_id', $episode->language_id) ?>
			<?php FormHelper::HiddenField('available_tag_types', $episode->available_tag_types) ?>
			<?php FormHelper::HiddenField('highlighted_cast_roles', $episode->highlighted_cast_roles) ?>
			<?php FormHelper::HiddenField('folders', $episode->folders) ?>
			<?php FormHelper::HiddenField('status', $episode->status) ?>
			<?php FormHelper::HiddenField('created_at', $episode->created_at) ?>
			<?php FormHelper::HiddenField('created_by', $episode->created_by) ?>
			<?php FormHelper::HiddenField('modified_at', $episode->modified_at) ?>
			<?php FormHelper::HiddenField('modified_by', $episode->modified_by) ?>
			<?php FormHelper::HiddenField('from', $from) ?>

			<?php /*FormHelper::TextField('episode_no', $episode->episode_no, "On Air Episode No.") */?>
			<?php /*FormHelper::TextField('original_episode_no', $episode->original_episode_no, "Original Episode No.")*/ ?>
            
            <?php
                $week_zh_encode = $episode->description; 
                $week_en_encode = $episode->description_en;
                $exad_zh_encode = $episode->description_short;
                $exad_en_encode = $episode->description_short_en;
					
		    	FormHelper::TextField('title', $episode->title, 
									  "Episode Title (Chinese)", $episode_diff['title']); 
				FormHelper::TextField('title_en', $episode->title_en, 
									  "Episode Title (English)", $episode_diff['title_en']);
				FormHelper::TextField('other_language_episodes', 
									  $episode->other_language_episodes, 
									  "Other Language Episode ID");
				FormHelper::TextArea('description', $week_zh_encode,
									 'Episode Synopsis <br>(tvbWeekly Chinese)', 
									 $episode_diff['description']);
				FormHelper::TextArea('description_en', $week_en_encode, 
									 'Episode Synopsis <br>(tvbWeekly English)', 
									 $episode_diff['description_en']);
				FormHelper::TextArea('description_short',$exad_zh_encode,
									 'Episode Synopsis <br>(EXAD Chinese)', 
									 $episode_diff['description_short']);
				FormHelper::TextArea('description_short_en', $exad_en_encode,
									 'Episode Synopsis <br>(EXAD English)', 
									 $episode_diff['description_short_en']);
									  ?> 
<?php 
if(in_array($data['_gn'],array('admin','proofreader','editor'))){
			FormHelper::DateTimePicker('publish_datetime', $episode->publish_datetime, 'Publish Date/Time (YYYY-MM-DD / HH:MM)', $episode_diff['publish_datetime'], 1);
			FormHelper::DateTimePicker('expire_datetime', $episode->expire_datetime, 'Expiry Date/Time (YYYY-MM-DD / HH:MM)', $episode_diff['expire_datetime'], 1);
}
?>
			<?php FormHelper::DateTimePicker('first_time_onair', $episode->first_time_onair, 'On Air Date/Time (YYYY-MM-DD / HH:MM)', $episode_diff['first_time_onair']) ?>
            <?php // bug 3979 FormHelper::TextField('average_rating', $episode->average_rating, "Average Rating", $episode_diff['average_rating'],'number') ?>
			<?php 
                $msg_zh_encode = $episode->editor_message;
               
                $msg_en_encode = $episode->episode_language[$en_language_id]->editor_message;
			
				FormHelper::TextArea('editor_message', $msg_zh_encode,
									 'Editor\'s Message(Chinese)', 
									 $episode_diff['average_rating']);
				FormHelper::TextArea('editor_message_en', $msg_en_encode,
									 'Editor\'s Message(English)', 
									 $episode_diff['average_rating']);
			?> 
		</fieldset>

		<div id='anchor-cast'></div>
    	<fieldset class="casts ui-widget-content">
			<legend class="ui-widget-header">Cast <?php if ( $act == 'view' && in_array(true, $cast_highlight)) echo 'Update'; ?></legend>
			<?php FormHelper::GlobalNameInput($arr_cast, $cast_list, 0, '', $cast_highlight) ?>
		</fieldset>

		<div id='anchor-crew'></div>
    	<fieldset class="casts ui-widget-content">
			<legend class="ui-widget-header">Crew <?php if ( $act == 'view' && in_array(true, $crew_highlight) ) echo 'Update'; ?></legend>
			<?php FormHelper::GlobalNameInput($arr_crew, $crew_list, 0, '', $crew_highlight) ?>
		</fieldset>

<!-- 
	<a id="anchor-video"></a>
    <fieldset class="ui-widget-content">
        <legend class="ui-widget-header">Video</legend>

        <div class="ui-helper-clearfix folder-manager-box">
            <a href="#video-episode-<?php echo $episode->id;?>" class="btn ui-state-default ui-corner-all folder-manager-btn" title="Manage Folders">Manage Folders</a>
        </div>
<?php
if (isset($video_folder_list['folders']) && (count($video_folder_list['folders']) > 0) ) {
	FormHelper::RedCallers('fordersrs', $video_folder_list);
}
?>
    </fieldset>
-->
	<div id='anchor-image'></div>
    <fieldset class="ui-widget-content">
        <legend class="ui-widget-header">Image</legend>
		<?php if($act != 'view') { ?>
        <div class="ui-helper-clearfix folder-manager-box">
            <a href="#image-episode-<?php echo $episode->id;?>" class="btn ui-state-default ui-corner-all folder-manager-btn" title="Manage Folders">Manage Folders</a>
		</div>
		<?php } ?>
		<?php
        if (isset($image_folder_list['folders']) && (count($image_folder_list['folders']) > 0) ) {
            FormHelper::RedCallers('fordersrs', $image_folder_list);
        }
        ?>
	</fieldset>

        <fieldset class="ui-widget-content">
            <legend class="ui-widget-header">Record Status and Access Log</legend>
                <?php FormHelper::ProtectedField(AC::$op_status[$episode->status], 'Record Status', true) ?>
                <?php FormHelper::ProtectedField($episode->created_by, 'Created By') ?>
                <?php FormHelper::ProtectedField($episode->created_at, 'Created At') ?>
                <?php FormHelper::ProtectedField($episode->modified_by, 'Modified By') ?>
                <?php FormHelper::ProtectedField($episode->modified_at, 'Modified At') ?>
        </fieldset>

        <?php FormHelper::DisplayPublishLog($data['publishLog']); ?>
		<span class="buttons">
<?php
switch ($act) {
case 'add':
	($step == 2) ? 	FormHelper::SubmitButton('update', 'Update') : FormHelper::SubmitButton('create', 'Create'); 
	FormHelper::ResetButton('reset', 'Reset');
	($step == 2) ? FormHelper::Button('top', 'Top') : "";
	break;
case 'clone':
	FormHelper::SubmitButton('update', 'Update');
	FormHelper::ResetButton('reset', 'Reset');
	break;
case 'edit':
	FormHelper::SubmitButton('update', 'Update');
	FormHelper::ResetButton('reset', 'Reset');
	FormHelper::Button('back', 'Back to list');
	FormHelper::Button('top', 'Top');
	break;
case 'view':
	/** modified by vanni for bug no:260 **/
	if($publish ==1){
        CommonUtils::showUnpublishButton($data, $data['_g']);
    }else{
		if($episode->status == AC::STATUS_DELETED){
			FormHelper::SubmitButton('recover', 'Recover');
		}else{
			FormHelper::SubmitButton('edit', 'Edit');
			# FormHelper::SubmitButton('clone', 'Clone');
            CommonUtils::showDeleteButton($data['publishLog']);
            CommonUtils::showPublishButton(!empty($programme->premium_flag), $data['_g']);
        }
	}
	FormHelper::Button('back', 'Back to list');
	FormHelper::Button('top', 'Top');
	break;
}
?>

		</span>
        
	<?php FormHelper::EndForm() ?>

	</div>

</div>


<script>
    TVB.Pdes.oValidation = {
        form: document.forms['ane'],
        rules: <?php echo json_encode($validation_rules); ?>
    };
</script>

